package com.arvato.right.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.common.util.ContantFinalUtil;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.right.bean.bean_do.WxworkRightRoleDo;
import com.arvato.right.bean.bean_vo.WxworkRightRoleVo;
import com.arvato.right.service.WxworkRightRoleService;

@Controller
@RequestMapping("/admin/right/role")
public class RightRoleController {
	
	@Autowired
	private WxworkRightRoleService wxworkRightRoleService;
	
	/**
	 * 查询角色列表
	 * 编辑用户的时候 查询的角色列表 需要有默认选中
	 * @param param
	 * param 中的参数：
	 * start
	 * limit
	 * userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData listRoleByQuery(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData data = wxworkRightRoleService.listRoleByQuery(query);
		if(query.get("userId") != null){
			// 当前用户的列表需要默认选中
			List<WxworkRightRoleVo> roleList = wxworkRightRoleService.listRoleByUser(query.get("userId") + "");
				for(WxworkRightRoleVo role1 : roleList){
					for(Object obj : data.getData()){
						WxworkRightRoleVo role2 = (WxworkRightRoleVo)obj;
						if(role1.getRoleId().equals(role2.getRoleId())){
							role2.setChecked(true);
						}
					}
				}
		}
		return data;
	}
	
	/**
	 * 查询角色详情
	 * @param param
	 * param 中的参数 roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result getRoleByQuery(String roleId){
		WxworkRightRoleVo role = wxworkRightRoleService.getRoleById(roleId);
		return Result.success(role);
	}
	
	/**
	 * 新增角色
	 * @param role
	 * @param resourceIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(WxworkRightRoleDo role, String[] resourceIds){
		try{
			role.setLastoperDate(new Date());
			int row = wxworkRightRoleService.insert(role, resourceIds);
			if(row == 1){
				return Result.success("新增成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("新增失败");
	}
	
	/**
	 * 更新
	 * @param role
	 * @param resourceId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result update(WxworkRightRoleDo role, String[] resourceIds){
		if(ContantFinalUtil.MAX_ROLE_ID.equals(role.getRoleId())) {
			return Result.fail("超级管理员角色,只能由开发在数据库里修改。");
		}
		try{
			int row = wxworkRightRoleService.update(role, resourceIds);
			if(row == 1){
				return Result.success("修改成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return 	Result.fail("修改失败");
	}
	/**
	 * 删除角色
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String roleId){
		// 超级管理员角色 不可删除
		if(ContantFinalUtil.MAX_ROLE_ID.equals(roleId)) {
			return Result.fail("超级管理员角色 不可删除");
		}
		try {
			int row = wxworkRightRoleService.delete(roleId);
			if(row == 1){
				return Result.success("删除成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("删除失败");
	}
}
